#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
控制面板模块
包含算法选择、参数设置等控制功能
"""

from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QGroupBox, QComboBox,
                            QLabel, QPushButton, QSpinBox, QDoubleSpinBox,
                            QCheckBox, QFormLayout, QProgressBar)
from PyQt5.QtCore import Qt, pyqtSignal

class ControlPanel(QWidget):
    """控制面板类，包含算法选择和参数设置"""
    
    # 定义信号
    algorithm_changed = pyqtSignal(str)  # 算法改变信号
    execute_clicked = pyqtSignal(dict)   # 执行按钮点击信号
    
    def __init__(self):
        """初始化控制面板"""
        super().__init__()
        
        # 设置面板样式
        self.setStyleSheet("""
            QGroupBox {
                background-color: white;
                border: 1px solid #cccccc;
                border-radius: 5px;
                margin-top: 1ex;
                font-weight: bold;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 3px 0 3px;
            }
            QPushButton {
                background-color: #2980b9;
                color: white;
                border: none;
                padding: 8px;
                border-radius: 4px;
            }
            QPushButton:hover {
                background-color: #3498db;
            }
            QPushButton:pressed {
                background-color: #2475a8;
            }
            QComboBox {
                padding: 5px;
                border: 1px solid #bdc3c7;
                border-radius: 3px;
            }
        """)
        
        # 创建主布局
        self.main_layout = QVBoxLayout(self)
        self.main_layout.setSpacing(10)
        
        # 创建各个控制组件
        self._create_algorithm_group()
        self._create_parameters_group()
        self._create_execution_group()
        
        # 添加弹性空间
        self.main_layout.addStretch()
        
    def _create_algorithm_group(self):
        """创建算法选择组"""
        group = QGroupBox("算法选择")
        layout = QFormLayout()
        
        # 算法选择下拉框
        self.algorithm_combo = QComboBox()
        self.algorithm_combo.addItems(["α算法", "Heuristic Miner"])
        self.algorithm_combo.currentTextChanged.connect(self._on_algorithm_changed)
        layout.addRow("选择算法:", self.algorithm_combo)
        
        group.setLayout(layout)
        self.main_layout.addWidget(group)
        
    def _create_parameters_group(self):
        """创建参数设置组"""
        self.parameters_group = QGroupBox("参数设置")
        self.parameters_layout = QFormLayout()
        
        # α算法参数
        self.alpha_params = {
            'event_threshold': QSpinBox(),
            'concurrent_detection': QCheckBox("启用"),
            'loop_detection': QCheckBox("启用")
        }
        self.alpha_params['event_threshold'].setRange(1, 1000)
        self.alpha_params['event_threshold'].setValue(50)
        
        # Heuristic Miner参数
        self.heuristic_params = {
            'dependency_threshold': QDoubleSpinBox(),
            'and_threshold': QDoubleSpinBox(),
            'loop_threshold': QDoubleSpinBox()
        }
        for param in self.heuristic_params.values():
            param.setRange(0, 1)
            param.setSingleStep(0.1)
            param.setValue(0.5)
        
        # 初始显示α算法参数
        self._show_alpha_parameters()
        
        self.parameters_group.setLayout(self.parameters_layout)
        self.main_layout.addWidget(self.parameters_group)
        
    def _create_execution_group(self):
        """创建执行控制组"""
        group = QGroupBox("执行控制")
        layout = QVBoxLayout()
        
        # 进度条
        self.progress_bar = QProgressBar()
        self.progress_bar.setTextVisible(True)
        self.progress_bar.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.progress_bar)
        
        # 执行按钮
        self.execute_button = QPushButton("执行挖掘")
        self.execute_button.setMinimumHeight(40)
        self.execute_button.clicked.connect(self._on_execute)
        layout.addWidget(self.execute_button)
        
        group.setLayout(layout)
        self.main_layout.addWidget(group)
        
    def _show_alpha_parameters(self):
        """显示α算法参数"""
        # 清空现有参数
        self._clear_parameters_layout()
        
        # 添加α算法参数
        self.parameters_layout.addRow("事件阈值:", self.alpha_params['event_threshold'])
        self.parameters_layout.addRow("并发检测:", self.alpha_params['concurrent_detection'])
        self.parameters_layout.addRow("循环检测:", self.alpha_params['loop_detection'])
        
    def _show_heuristic_parameters(self):
        """显示Heuristic Miner参数"""
        # 清空现有参数
        self._clear_parameters_layout()
        
        # 添加Heuristic Miner参数
        self.parameters_layout.addRow("依赖阈值:", self.heuristic_params['dependency_threshold'])
        self.parameters_layout.addRow("AND阈值:", self.heuristic_params['and_threshold'])
        self.parameters_layout.addRow("循环阈值:", self.heuristic_params['loop_threshold'])
        
    def _clear_parameters_layout(self):
        """清空参数布局"""
        while self.parameters_layout.count():
            item = self.parameters_layout.takeAt(0)
            if item.widget():
                item.widget().hide()
                
    def _on_algorithm_changed(self, algorithm):
        """算法选择改变时的处理函数"""
        if algorithm == "α算法":
            self._show_alpha_parameters()
        else:
            self._show_heuristic_parameters()
        self.algorithm_changed.emit(algorithm)
        
    def _on_execute(self):
        """执行按钮点击处理函数"""
        # 获取当前参数
        algorithm = self.algorithm_combo.currentText()
        parameters = {}
        
        if algorithm == "α算法":
            parameters = {
                'event_threshold': self.alpha_params['event_threshold'].value(),
                'concurrent_detection': self.alpha_params['concurrent_detection'].isChecked(),
                'loop_detection': self.alpha_params['loop_detection'].isChecked()
            }
        else:
            parameters = {
                'dependency_threshold': self.heuristic_params['dependency_threshold'].value(),
                'and_threshold': self.heuristic_params['and_threshold'].value(),
                'loop_threshold': self.heuristic_params['loop_threshold'].value()
            }
        
        # 发送执行信号
        self.execute_clicked.emit({
            'algorithm': algorithm,
            'parameters': parameters
        })
        
    def set_progress(self, value):
        """设置进度条值"""
        self.progress_bar.setValue(value)
        
    def reset_progress(self):
        """重置进度条"""
        self.progress_bar.setValue(0) 